package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectStatus;
import com.abvnet.project.water.project.service.IProjectInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目诊断信息操作处理
 * 
 * @author duj
 * @date 2019-07-23
 */
@Controller
@RequestMapping("/project/projectDiagnosis")
public class ProjectDiagnosisController extends BaseController {
    private String prefix = "project/projectDiagnosis";
	
    @Autowired
    private IProjectInfoService projectInfoService;
	
	@RequiresPermissions("project:projectDiagnosis:list")
	@GetMapping()
	public String projectDiagnosis()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询项目诊断列表
	 */
	@RequiresPermissions("project:projectDiagnosis:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo) {
		if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
			projectInfo.setCreateBy(ShiroUtils.getLoginName());
			projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
		}
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
	}

	/**
	 * 修改项目诊断
	 */
	@GetMapping("/edit/{projectId}")
	public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(projectId);
        mmap.put("projectInfo", projectInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目诊断
	 */
	@RequiresPermissions("project:projectDiagnosis:edit")
	@Log(title = "项目诊断", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
    public AjaxResult editSave(ProjectInfo projectInfo) {
        ProjectInfo pi = projectInfoService.selectProgressCodeById(projectInfo.getProjectId());
//        if (StringUtils.equals(ProjectStatus.INFO.getCode(),pi.getProgressCode())) {
//            projectInfo.setProgressCode(ProjectStatus.DIAG.getCode());
//        }
		if (Integer.parseInt(pi.getProgressCode()) < Integer.parseInt(ProjectStatus.DIAG.getCode()))
			projectInfo.setProgressCode(ProjectStatus.DIAG.getCode());

        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }

    @RequiresPermissions("project:projectDiagnosis:list")
    @GetMapping("/view/{projectId}")
    public String view(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(projectId);
        mmap.put("projectInfo", projectInfo);
        return prefix + "/view";
    }
}